/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excel;

import aaa.Configurator;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermEnum;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 *
 * @author Bashev
 */
public class ExcelFile {
        
        private IndexReader ir;
    
        public ExcelFile(IndexReader ir){
            this.ir = ir;
        }
        
        public void createXLSFile(String fileName) throws FileNotFoundException, IOException{
            String xlsFileName = Configurator.getConfigProperty("xls_path") + "//"+fileName+".xls";
            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("new sheet");
            HSSFRow rowhead = sheet.createRow(0);
            rowhead.createCell(0).setCellValue("Word");
            rowhead.createCell(1).setCellValue("Count");
            TermEnum terms = ir.terms(); 
                int i = 1;
                while (terms.next()) {
                    String[] contents = new String[2];
                    contents[0] = terms.term().text();
                    contents[1] = Integer.toString(terms.docFreq());
                    HSSFRow row = sheet.createRow(i);
                    row.createCell(0).setCellValue(contents[0]);
                    row.createCell(1).setCellValue(contents[1]);
                    i++;
                
            }
            FileOutputStream fileOut = new FileOutputStream(xlsFileName);
                hwb.write(fileOut);
                fileOut.close();
        }
        
}
